const path = require('path')
const withLess = require('@zeit/next-less')
const util = require('util')

if (typeof require !== 'undefined') {
  require.extensions['.less'] = () => {}
}

module.exports = withLess({
	useFileSystemPublicRoutes: false,
	cssModules: true, // 开启css modules
  cssLoaderOptions: {
    importLoaders: 1,
    localIdentName: '[local]___[hash:base64:5]',
  },
	webpack(config, { dev, isServer }) {
		const eslintRule = {
			// enforce: 'pre',
			test: /\.(js|jsx)$/,
			loader: 'eslint-loader',
			exclude: [/node_modules/]
		}
		config.module.rules.push(eslintRule)

		delete config.module.rules[0].exclude
		delete config.module.rules[0].include
		
		if (isServer) {
			config.module.rules.unshift({
				test: /\.css$/,
				use: [
					{
						loader: './build.idea/loaders/css.js',
						options: { abc: 2233 }
					}
				]
			})
		} else {
			config.module.rules.unshift({
				test: /\.css$/,
				use: [
					{
						loader: 'style-loader'
					},
					{
						loader: 'css-loader'
					}
				]
			})

			config.infrastructureLogging = {
				level: 'log',
				debug: [
					(name) => name.contains('less-loader')
				]
			}
		}
		
		console.log(`--------------- isServer: ${isServer}, dev: ${dev} ---------------`)
		// console.log(util.inspect(config, {depth: null}))

		return config
	}
})